package me.luger.project.picc.controller.api;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.project.picc.entity.ActivityEnum;
import me.luger.project.picc.entity.PrizeList;
import me.luger.project.picc.entity.picc.PolicyResult;
import me.luger.project.picc.service.PrizeListService;
import me.luger.project.picc.service.picc.PiccService;
import me.luger.project.picc.service.picc.TongjiService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ResponseBody;

import me.luger.project.picc.service.InsuranceService;
import me.luger.project.picc.entity.Insurance;

import java.util.Map;

/**
 * 投保有奖 controller
 */
@Controller
@RequestMapping("/api/picc/insurance")
public class InsuranceApiController extends BaseController {

    @Autowired
    private InsuranceService insuranceService;

    @Autowired
    private PrizeListService prizeListService;

    @Autowired
    private PiccService piccService;

    @Autowired
    private TongjiService tongjiService;

    @RequestMapping(value = "/save",name = "添加或者更新学点健康知识吧接口")
    @ResponseBody
    public Object save(String openId,
                       String pid,
                       String userName){
        int bizId = 0;
        try{
//            Insurance insurance = insuranceService.fetchByPid(pid);
//            if(null != insurance){
//                return Result.success().addDateItem("bizId",insurance.getId());
////                return Result.info(-1,"该保单号已参加过该活动");
//            }

            PolicyResult policyResult = piccService.checkPolicyResult(openId,userName,pid);

            boolean exist = policyResult.isExist();
            boolean normal = policyResult.isNormal();
            boolean dateRight = policyResult.isDateRight();
            boolean overOneYear = policyResult.isOverOneYear();
            boolean newRet = policyResult.isNewRet();
            boolean self = policyResult.isSelf();
            boolean periodPay = policyResult.isPeriodPay();

            if(!exist){
                return Result.info(-2,"您填写的保单不存在，请重新填写");
            }

            if(!normal){
                return Result.info(-2,"您的保单已经超过有效期，请重新填写");
            }

            if(!dateRight){
                return Result.info(-2,"您的保单不符合本活动对承保期（5月18日-7月18日）的要求");
            }
            if(!overOneYear){
                return Result.info(-2,"您的保单是一年期以下产品，不符合活动要求");
            }
            if(!newRet){
                return Result.info(-2,"您的保单不是新契约回访成功，不符合活动要求");
            }
            if(!self){
                return Result.info(-2,"您填写的投保人与保单信息不一致，请重新填写");
            }

            Insurance insurance = new Insurance();
            if(!exist || !dateRight || !overOneYear || !newRet || !self){
//                return Result.info(-2,"保单号校验不通过").addDateItem("exist",exist)
//                        .addDateItem("dateRight",dateRight)
//                        .addDateItem("newRet",newRet)
//                        .addDateItem("self",self)
//                        .addDateItem("overOneYear",overOneYear);
                insurance.setDoPrizeStatus(0);
            }else{
                if(periodPay){//投保5000以上
                    insurance.setDoPrizeStatus(1);
                }else{//投保5000以下
                    insurance.setDoPrizeStatus(2);
                }

            }


            insurance.setOpenId(openId);
            insurance.setPid(pid);
            insurance.setUserName(userName);
            insurance = insuranceService.insert(insurance);

            tongjiService.addUserCount(openId, ActivityEnum.INSURANCE,0);

            bizId = insurance.getId();
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("bizId",bizId);
    }

    @RequestMapping(value = "/status",name = "用户是否参加客户满意度有奖调查抽奖")
    @ResponseBody
    public Object status(String openId){
        Map<String,Object> result = new HashedMap();
        boolean hasSubmitQuestion = false,hasDoDraw=false,hasGetPrize=false,hasCompleteAdress=false;
        int bizId = 0;
        String prizeSecrect = "";
        try{
            Insurance insurance = insuranceService.fetchByopenId(openId);
            if(null != insurance){
                bizId = insurance.getId();
                hasSubmitQuestion=true;
            }

            PrizeList prizeList = prizeListService.fetchByOpenIdAndTypeAndBizId(openId, ActivityEnum.INSURANCE.getActivityId(),bizId);

            //用户是否抽奖
            if(null != prizeList){
                hasDoDraw = true;
            }

            //如果用户抽过奖返回用户是否中奖。
            if(hasDoDraw && prizeList.getPrizeResult() == 1){
                hasGetPrize = true;
            }

            if(hasGetPrize && StringUtil.isNotBlank(prizeList.getZcode()) && StringUtil.isNotBlank(prizeList.getAddress()) && StringUtil.isNotBlank(prizeList.getPhoneNum())){
                hasCompleteAdress = true;
            }

            if(hasGetPrize && !hasCompleteAdress){
                prizeSecrect = prizeList.getPrizeSecrect();
            }

            result.put("hasSubmitQuestion",hasSubmitQuestion);
            result.put("hasDoDraw",hasDoDraw);
            result.put("hasGetPrize",hasGetPrize);
            result.put("hasCompleteAdress",hasCompleteAdress);
            result.put("bizId",bizId);
            result.put("prizeSecrect",prizeSecrect);

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success(result);
    }

    @RequestMapping(value = "/checkInsurance",name = "校验保单接口")
    @ResponseBody
    public Object checkInsurance(String pid,String userName){
        PolicyResult policyResult = null;
        try{
            policyResult = piccService.checkPolicyResult("1",userName,pid);
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("policyResult",policyResult);
    }

    @RequestMapping(value = "",name = "接口描述")
    @ResponseBody
    public Object demo(){
        try{

        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }
}
